package com.movitech.mobile.cz.base.common.utils;

import java.util.Arrays;
import java.util.List;

import org.activiti.bpmn.model.ActivitiListener;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.FieldExtension;
import org.activiti.bpmn.model.MultiInstanceLoopCharacteristics;
import org.activiti.bpmn.model.ParallelGateway;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ActivitiUtils {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 任务节点-无用户
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	public static UserTask createNoUserTask(String key, String name) {
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(key);
		return userTask;
	}

	/**
	 * 任务节点-锁定者
	 * 
	 * @param id
	 * @param name
	 * @param userId
	 * @return
	 */
	public static UserTask createAssigneeTask(String key, String name, String userId, Integer nodeCountsignFlag) {
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(key);

		/// 会签
		if (NumberUtils.toInt(nodeCountsignFlag + "") == ActivitiConstant.YES_INTEGER) {
			userTask.setAssignee("${"+key+"user}");
			MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = new MultiInstanceLoopCharacteristics();
			multiInstanceLoopCharacteristics.setCompletionCondition("${nrOfCompletedInstances/nrOfInstances >= 1 }");
			multiInstanceLoopCharacteristics.setElementVariable(key+"user");
			multiInstanceLoopCharacteristics.setInputDataItem("lst"+key+"Users");
			userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);
		} else {
			userTask.setAssignee(userId);
		}
		return userTask;
	}

	/**
	 * 任务节点-用户
	 * 
	 * @param id
	 * @param name
	 * @param candidateUser
	 * @return
	 */
	public static UserTask createCandidateUserTask(String key, String name, String candidateUser) {
		List<String> lstCandidateUsers = null;
		if (StringUtils.isNotEmpty(candidateUser)) {
			lstCandidateUsers = com.movitech.mobile.cz.base.common.utils.StringUtils.getListBySplitStr(candidateUser);
			if (lstCandidateUsers != null && lstCandidateUsers.size() == 0)
				lstCandidateUsers = null;
		}
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(key);
		userTask.setCandidateUsers(lstCandidateUsers);
		return userTask;
	}

	/**
	 * 任务节点-组
	 * 
	 * @param id
	 * @param name
	 * @param candidateGroup
	 * @return
	 */
	public static UserTask createCandidateGroupsTask(String key, String name, String candidateGroups) {
		List<String> lstCandidateGroups = null;
		if (StringUtils.isNotEmpty(candidateGroups)) {
			lstCandidateGroups = com.movitech.mobile.cz.base.common.utils.StringUtils.getListBySplitStr(candidateGroups);
			if (lstCandidateGroups != null && lstCandidateGroups.size() == 0)
				lstCandidateGroups = null;
		}
		UserTask userTask = new UserTask();
		userTask.setName(name);
		userTask.setId(key);
		userTask.setCandidateGroups(lstCandidateGroups);
		return userTask;
	}

	/**
	 * 连线
	 * 
	 * @param from
	 * @param to
	 * @param name
	 * @param conditionExpression
	 * @return
	 */
	public static SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
		SequenceFlow flow = new SequenceFlow();
		flow.setSourceRef(from);
		flow.setTargetRef(to);
		flow.setName(name);
		if (StringUtils.isNotEmpty(conditionExpression)) {
			flow.setConditionExpression(conditionExpression);
		}
		return flow;
	}

	/***
	 * 连线
	 * @param from
	 * @param to
	 * @param name
	 * @param conditionExpression
	 * @param event
	 * @param listenCalss
	 * @param lstFieldExtensions
	 * @return
	 */
	public static SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression,String event,String listenCalzz,List<FieldExtension> lstFieldExtensions) {
		SequenceFlow flow = new SequenceFlow();
		flow.setSourceRef(from);
		flow.setTargetRef(to);
		flow.setName(name);
		if (StringUtils.isNotEmpty(conditionExpression)) {
			flow.setConditionExpression(conditionExpression);
		}

		ActivitiListener activitiListener = new ActivitiListener();
		activitiListener.setEvent("take");
		activitiListener.setImplementationType("class");
		activitiListener.setImplementation("com.movitech.mobile.cz.base.purchase.listener.FlowStatusExecutionListener");
		activitiListener.setFieldExtensions(lstFieldExtensions);
		flow.setExecutionListeners(Arrays.asList(new ActivitiListener[] {activitiListener}));
		return flow;
	}
	
	/**
	 * 排他网关
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	public static ExclusiveGateway createExclusiveGateway(String key, String name) {
		ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
		exclusiveGateway.setId(key);
		exclusiveGateway.setName(name);
		return exclusiveGateway;
	}

	/**
	 * 并行网关
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	public static ParallelGateway createParallelGateway(String key, String name) {
		ParallelGateway gateway = new ParallelGateway();
		gateway.setId(key);
		gateway.setName(name);
		return gateway;
	}

	/**
	 * 开始节点
	 * 
	 * @return
	 */
	public static StartEvent createStartEvent(String key, String name) {
		StartEvent startEvent = new StartEvent();
		startEvent.setId(key);
		startEvent.setName(name);
		return startEvent;
	}

	/**
	 * 结束节点
	 * 
	 * @return
	 */
	public static EndEvent createEndEvent(String key, String name) {
		EndEvent endEvent = new EndEvent();
		endEvent.setId(key);
		endEvent.setName(key);
		return endEvent;
	}
}
